#!/usr/bin/env python
"""
-->
Directories = "Defines the directories you like to search through."
Path = {'type': "System.String", 'description': "Path where you like to search the expected files.", 'validation': "IsPath"}
IgnoreSubDirectories = {'type': "System.Boolean", 'description': "If true all subfolders will be ignored.", 'defaultValue': "false"}
-->

-->
FileSettings = "Defines the extensions of the files you like to search for."
FileExtension = {'type': "System.String", 'description': "Extension of the file you search for. (example: *.txt || *[0-9]* )"}
-->

<--
Output = "Defines all found files with the expected data."
AbsolutePath = {'type': "System.String", 'description': "The absolute path to the element."}
DirectoryName = {'type': "System.String", 'description': "The relative path to the element."}
BaseName = {'type': "System.String", 'description': "The base name of the element without parent directories."}
FileExtension = {'type': "System.String", 'description': "The file extension."}
IsFile = {'type': "System.Boolean", 'description': "If true this element is a file. If false this element is a directory."}
<--
"""

from MyClever.Lib import MissionPlugin
import os
import glob

__author__ = "Mathias Mielitz"
__copyright__ = "Copyright 2012, MyCleverFriend"
__credits__ = ["Mathias Mielitz"]
__license__ = "GPL"
__version__ = "1.0.0.0"
__maintainer__ = "Mathias Mielitz"
__email__ = "m.mielitz@adrabbit.de"
__status__ = "Production"
__description__ = "Return a list containing the names of the entries in the directory."

class ListDir(MissionPlugin):
	def Work(self):

		# get all directorie elements
		directoryPackage = self.GetDataReader("Directories")
		while directoryPackage.Read():
			self.AddNewLoggingMessage("Directories", "search in path: " + directoryPackage["Path"].ToString());
			
			for dirElements in os.walk(directoryPackage["Path"].ToString()):
				
				# set fileSettings
				settingsPackage = self.GetDataReader("FileSettings")
				while settingsPackage.Read():
					files = glob.glob(os.path.join(dirElements[0], settingsPackage["FileExtension"].ToString()))

					for fileOn in files:
						fileName, fileExtension = os.path.splitext(fileOn);
						fileExtension = fileExtension[1:];

						newRow = self.CreateNewRowFromPackage("Output");
						newRow["AbsolutePath"] = fileOn;
						newRow["DirectoryName"] = dirElements[0];
						newRow["BaseName"] = os.path.basename(fileOn);
						newRow["FileExtension"] = fileExtension;
						newRow["IsFile"] = os.path.isfile(fileOn);
						self.AddNewRowToPackage("Output", newRow);